﻿using BasicInformation.ApiWrite.Application.Command.Inventory;
using BasicInformation.Domain;
using BasicInformation.ErrorCode;
using BasicInformation.Infrastructure.Implement;
using MediatR;

namespace BasicInformation.ApiWrite.Application.Handler.Inventory
{
    public class DelInventoryHandler : IRequestHandler<DelInventoryCommand, ApiResult<int>>
    {
        private readonly DatabaseContext db;
        private readonly ILogger<DelInventoryHandler> logger;

        public  DelInventoryHandler(DatabaseContext db,ILogger<DelInventoryHandler> logger)
        {
            this.db = db;
            this.logger = logger;
        }

        public async Task<ApiResult<int>> Handle(DelInventoryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var model=db.SqlSugar.Queryable<InventorysModel>().Where(x=>x.InventoryId==request.InventoryId).First();
            model.IsDel=true;
            int r=await db.SqlSugar.Updateable(model).ExecuteCommandAsync();
            if (r > 0)
            {
                result.Msg = "库存盘点删除成功";
                result.Data = r;
                result.Code = ApiEnum.OK;
                logger.LogInformation(result.Msg);
            }
            else
            {
                result.Msg = "库存盘点删除失败";
                result.Data = r;
                result.Code = ApiEnum.Fail;
                logger.LogError(result.Msg);
            }
            return await Task.FromResult(result);
        }
    }
}
